import { request } from "utils/request.js";
import regeneratorRuntime from "utils/runtime.js";
var flashEvent = require('/utils/FlashEvent.js')
App({
  onLaunch: function(options) {
    //注册音频事件
    this.registerAudioContext();
    //监听播放id变化
    var that=this;
    this.watchplayingId(that.watchBackplayingId);
    flashEvent.register(flashEvent.EVENT_KEYS.isplay, this, function (data) {
      this.handleAudioPlay()
    })
  },
  onShow: function(options) {

  },
  onHide: function() {

  },
  onError: function(msg) {

  },
  onPageNotFound: function(options) {

  },
  //重新获取播放音频src
  getMusicSrc(){
    let {playingId}=this.globalData;
    //console.log("hans")
    this.audioCtx.src = 'https://music.163.com/song/media/outer/url?id='+playingId;
    this.audioCtx.title="StartLine音乐"
  },
  //播放状态控制
  handleAudioPlay(){
    let {zzPlay}=this.globalData;
    if(zzPlay){
      getApp().audioCtx.pause();
      zzPlay=false
    }
    else{
      zzPlay=true
      getApp().audioCtx.play();
    }
    this.globalData.zzPlay=zzPlay;
  },
  //是否为单曲循环
  isloop(isloop){
    this.getMusicSrc()
  },
  //调试函数
  seek(){
    this.audioCtx.seek(230)
  }, 

  registerAudioContext(){
    //创建音频实列
    this.audioCtx= wx.getBackgroundAudioManager();
    //this.audioCtx.src = '';
    this.audioCtx.coverImgUr="icon/icons/StartLine.jpg"
    this.audioCtx.title="StartLine音乐"
    this.audioCtx.autoplay=true;
    this.audioCtx.loop=false;
    //监听音频播放事件
    this.audioCtx.onPlay(() => {
      this.globalData.zzPlay=true,
      this.globalData.animation='running'
      //console.log('开始播放');
    })
    //监听暂停事件
    this.audioCtx.onPause(()=>{
      this.globalData.zzPlay=false
      this.globalData.animation='paused'
    }) 
    //监听播放完的事件
    this.audioCtx.onEnded(()=>{
      this.globalData.onEnded+=1;
    })

    //音频进度更新事件 获取音频时长
    this.audioCtx.onTimeUpdate(()=>{
      //获取音频长度
      let audAllTime=this.audioCtx.duration;
      //console.log(audAllTime);
      let allmin=parseInt(audAllTime/60);
      let allsec=parseInt(audAllTime%60);
      //进度条控制事件和播放时间控制
      //已经播放时间
      let palytimed=this.audioCtx.currentTime;
      //console.log(palytimed);
      let playmined=parseInt(palytimed/60);
      let playseced=parseInt(palytimed%60);
      //进度条
      let Percentage=(palytimed/audAllTime)*100;
      this.globalData.data={
        duration:'0'+allmin+':'+allsec,
        playtime: '0'+playmined+':'+playseced,
        playingTime:Percentage
      }
    })
  },
//监听播放时间变化
watchdata:function(method){
    var obj = this.globalData;
    Object.defineProperty(obj,"data", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._name = value;
        method(value);
      },
      get:function(){
        return this._name
      }
    })
  }, 
  //监听播放结束
  watchonEnded:function(method){
    var obj = this.globalData;
    Object.defineProperty(obj,"onEnded", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._onEnded = value;
        method(value);
      },
      get:function(){
        return this._onEnded
      }
    })
  },
  //监听播放状态
  watchIsplay:function(method){
    var obj = this.globalData;
    Object.defineProperty(obj,"zzPlay", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._isPlay = value;
        method(value);
      },
      get:function(){
        return this._isPlay
      }
    })
  },
  //监听播放id变化
  watchplayingId:function(method){
    var obj = this.globalData;
    Object.defineProperty(obj,"playingId", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._playingId = value;
        method(value);
      },
      get:function(){
        return this._playingId
      }
    })
  },
  //监听播放id变化
  watchBackplayingId: function (value){
    //console.log(value);
    this.getMusicSrc();
    this.globalData.playid=value
    this.globalData.isplayer=true
  },
  //监听给播放歌单的变化
  watchplayid:function(method){
    var obj = this.globalData;
    Object.defineProperty(obj,"playid", {
      configurable: true,
      enumerable: true,
      set: function (value) {
        this._playingId = value;
        method(value);
      },
      get:function(){
        return this._playingId
      }
    })
  },

  globalData: {
    //当前播放的id
    playingId:0,
    //播放id给歌单的变化
    playid:0,
    //播放给小播放器的id变化
    isplayer:false,
    playerData:{
      isplayer:false,
      playingId:0,
      playListId:0,
      albumListId:0,
    },
    //播放状态
    zzPlay:false,
    animation:'paused',
    //是否随机
    isshuffle:false,
    //播放完毕数值变化触发监听回调函数
    onEnded:0,
    //数据变化响应式传递
    data:{
      duration:'00:00',
      playtime:"00:00",
      playingTime:0,
    },
  }
});